﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;
namespace SIPO.Web.Forms.Administration.Catalog
{
   public partial class recTariffform : BasePage
    {
        private int id = -1;
        protected override void OnLoad(EventArgs e)
        {
            ddlCorporationAction.Enabled = false;
            ddlSector.Enabled = false;
            ddlSubSector.Enabled = false;
            ddlSubSubSector.Enabled = false;
            ddlImpostionBase.Enabled = false;
            ddlRegEmisiType.Enabled = false;

            txtPercentage.Enabled = false;
            txtMaxValue.Enabled = false;
            txtMinValue.Enabled = false;

            recTransactionTypeRepo recTransactionTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            List<recTransactionType> transtypes = recTransactionTypeDal.GetListIsTariff(1);
            ddlTransType.Items.Clear();
            transtypes.ForEach(delegate(recTransactionType transtype)
            {
                ddlTransType.Items.Add(new ListItem(transtype.TransTypeCode + " - " + transtype.TransTypeName, transtype.TransTypeID.ToString()));
            });
            ddlTransType.Items.Insert(0, new ListItem("-- pilih tipe transaksi  --", string.Empty));

         
            recImpositionBaseRepo recImpositionBaseDal = new recImpositionBaseRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            List<recImpositionBase> impbases = recImpositionBaseDal.GetList();
            ddlImpostionBase.Items.Clear();
            impbases.ForEach(delegate(recImpositionBase impbase)
            {
                ddlImpostionBase.Items.Add(new ListItem(impbase.ImpositonBaseName , impbase.ImpositionBaseID.ToString()));
            });
            ddlImpostionBase.Items.Insert(0, new ListItem("-- pilih dasar pengenaan  --", string.Empty));

            recCorporateActionTypeRepo recCorporateActionTypeDal = new recCorporateActionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            List<recCorporateActionType> corporats = recCorporateActionTypeDal.GetList();
            ddlCorporationAction.Items.Clear();
            corporats.ForEach(delegate(recCorporateActionType corporat)
            {
                ddlCorporationAction.Items.Add(new ListItem(corporat.CorporateActionName, corporat.CorporateActionTypeID.ToString()));
            });
            ddlCorporationAction.Items.Insert(0, new ListItem("-- pilih tipe aksi korporasi --", string.Empty));


           recRegistrationEmisionTypeRepo recRegistrationEmisionTypeDal  = new recRegistrationEmisionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
           List<recRegistrationEmisionType> regsemisi = recRegistrationEmisionTypeDal.GetList();
            ddlRegEmisiType.Items.Clear();
            regsemisi.ForEach(delegate(recRegistrationEmisionType regemisi)
            {
                ddlRegEmisiType.Items.Add(new ListItem(regemisi.RegistrationEmisionTypeName, regemisi.RegistrationEmisionTypeID.ToString()));
            });
            ddlRegEmisiType.Items.Insert(0, new ListItem("-- pilih tipe reg. emisi --", string.Empty));



            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                if (id > 0) // Edit an existing item
                {
                    
                    recSectorRepo recSubSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recSector> sectors = recSubSectorDal.GetList();
                    ddlSector.Items.Clear();
                    sectors.ForEach(delegate(recSector sector)
                    {
                        ddlSector.Items.Add(new ListItem(sector.SectorCode + " - " + sector.SectorName, sector.SectorID.ToString()));
                    });
                    ddlSector.Items.Insert(0, new ListItem("-- pilih sektor --", string.Empty));


                    recSubSectorRepo recSubSectorRepo = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recSubSector> subsectors = recSubSectorRepo.GetList();
                    ddlSubSector.Items.Clear();
                    subsectors.ForEach(delegate(recSubSector subsector)
                    {
                        ddlSubSector.Items.Add(new ListItem(subsector.SubSectorCode + " - " + subsector.SubSectorName, subsector.SubSectorID.ToString()));
                    });
                    ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sektor --", string.Empty));


                    recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recSubSubSector> subsubsectors = recSubSubSectorDal.GetList();
                    ddlSubSubSector.Items.Clear();
                    subsubsectors.ForEach(delegate(recSubSubSector subsubsector)
                    {
                        ddlSubSubSector.Items.Add(new ListItem(subsubsector.SubSubSectorCode + " - " + subsubsector.SubSubSectorName, subsubsector.SubSubSectorID.ToString()));
                    });
                    ddlSubSubSector.Items.Insert(0, new ListItem("-- pilih subsub sektor --", string.Empty));



                    recTariffRepo recTariffDal = new recTariffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recTariff recTariff = recTariffDal.GetById(id);
                    if (recTariff != null)
                    {
                        ddlCorporationAction.Enabled = true;
                        ddlImpostionBase.Enabled = true;
                        ddlRegEmisiType.Enabled = true;
                        ddlSector.Enabled = true;
                        ddlSubSector.Enabled = true;
                        ddlSubSubSector.Enabled = true;
                        ddlTransType.Enabled = true;
                        ddlRegEmisiType.Enabled = true;

                        txtValue.Enabled = true;
                        txtPercentage.Enabled = true;
                        txtMinValue.Enabled = true;
                        txtMaxValue.Enabled = true;
                        txtKelipatan.Enabled = true;

                        hfID.Value = id.ToString();
                        ddlSubSubSector.Items.FindByValue(recTariff.SubSubSectorID.ToString()).Selected = true;
                        ddlSubSector.Items.FindByValue(recTariff.SubSectorID.ToString()).Selected = true;
                        ddlSector.Items.FindByValue(recTariff.SectorID.ToString()).Selected = true;
                        //ddlRegEmisiType.Items.FindByValue(recTariff.RegistrationEmisionTypeID.ToString()).Selected = true;
                        ddlTransType.Items.FindByValue(recTariff.TransTypeID.ToString()).Selected = true;
                         CultureInfo ci = new CultureInfo("id-ID");
                         if (recTariff.Percentage != 0 && recTariff.Percentage != null)
                         {                             
                             txtPercentage.Text = recTariff.Percentage.Value.ToString();
                             if ((txtPercentage.Text.Contains("000") && !txtPercentage.Text.Contains(".000")))
                             {
                                 txtPercentage.Text = txtPercentage.Text.Replace("000", "");
                             }
                             else if (txtPercentage.Text.Contains(".0000"))
                             {
                                 txtPercentage.Text = txtPercentage.Text.Replace(".0000", "");
                             }
                             else if (txtPercentage.Text.Contains("0.0") && txtPercentage.Text.Contains("00") && !txtPercentage.Text.Contains("000"))
                             {
                                 txtPercentage.Text = txtPercentage.Text.Replace("00","");
                             }
                             else if (txtPercentage.Text.Contains("0.0"))
                             {
                                 txtPercentage.Text = txtPercentage.Text.TrimEnd('0');
                             }
                             
                             }
                         if (recTariff.Value != 0 && recTariff.Value != null)
                            txtValue.Text = recTariff.Value.Value.ToString("#,##.00", ci);
                         if (recTariff.MinValue != 0 && recTariff.MinValue != null)
                            txtMinValue.Text = recTariff.MinValue.Value.ToString("#,##.00", ci);
                         if (recTariff.MaxValue != 0 && recTariff.MaxValue != null)
                            txtMaxValue.Text = recTariff.MaxValue.Value.ToString("#,##.00", ci);
                         if (recTariff.Kelipatan != 0 && recTariff.Kelipatan != null)
                            txtKelipatan.Text = recTariff.Kelipatan.Value.ToString("#,##.00", ci);
                        ddlImpostionBase.Items.FindByValue(recTariff.ImpositionBaseID.ToString()).Selected = true;
                        chkIsActive.Checked = recTariff.IsActive ? recTariff.IsActive : false;
                        chkIsFormula.Checked = recTariff.IsFormula ? recTariff.IsFormula : false;
                        if (recTariff.StartDate != null)
                            txtStartDate.Text = recTariff.StartDate.ToString("dd/MM/yyyy");
                        if (recTariff.EndDate != null)
                            txtEndDate.Text = recTariff.EndDate.Value.ToString("dd/MM/yyyy");

                        if (recTariff.CorporateActionTypeID != null && recTariff.CorporateActionTypeID.ToString().Trim() != "")
                        {
                            ddlCorporationAction.Enabled = true;
                            ddlCorporationAction.Items.FindByValue(recTariff.CorporateActionTypeID.ToString()).Selected = true;
                        }
                        else
                            ddlCorporationAction.Enabled = false;

                        if(recTariff.RegistrationEmisionTypeID!=null && recTariff.RegistrationEmisionTypeID.ToString().Trim()!="")
                            ddlRegEmisiType.Items.FindByValue(recTariff.RegistrationEmisionTypeID.ToString()).Selected = true;
                       

                        if (txtValue.Text.Trim().Length == 0)
                            txtValue.Text = "0";

                        if (txtPercentage.Text.Trim().Length == 0)
                            txtPercentage.Text = "0";

                        if (txtMinValue.Text.Trim().Length == 0)
                            txtMinValue.Text = "0";

                        if (txtMaxValue.Text.Trim().Length == 0)
                            txtMaxValue.Text = "0";

                        if (txtKelipatan.Text.Trim().Length == 0)
                            txtKelipatan.Text = "0";

                        #region validation dropdownlist
                        //=====validation dropdownlist
                        if (recTariff.TransTypeID != 0)
                        {
                            recTransactionType objTransType = recTransactionTypeDal.GetById(recTariff.TransTypeID);

                            if (objTransType != null)
                            {
                                if (objTransType.TransTypeCode == 1)
                                {
                                    ddlCorporationAction.Enabled = false;

                                    //get sektor
                                    recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                    recSector objSector = recSectorDal.GetById(recTariff.SectorID);
                                    if (objSector != null)
                                    {
                                        if (objSector.SectorCode == "2" && objSector.SectorName.Trim().ToLower().Contains("pasar modal"))
                                        {
                                            //get sub sector

                                            recSubSector objSubSector = recSubSectorRepo.GetById(recTariff.SubSectorID);
                                            if (objSubSector != null)
                                            {
                                                if (objSubSector.SubSectorCode == "4" && objSubSector.SubSectorName.Trim().ToLower().Contains("emiten"))
                                                    ddlRegEmisiType.Enabled = true;
                                                else
                                                {
                                                    ddlRegEmisiType.SelectedIndex = 0;
                                                    ddlRegEmisiType.Enabled = false;
                                                }
                                            }

                                        }
                                        else
                                        {
                                            ddlRegEmisiType.SelectedIndex = 0;
                                            ddlRegEmisiType.Enabled = false;
                                        }
                                    }

                                }
                                else
                                {
                                    if (objTransType.TransTypeCode == 4)
                                    {
                                        ddlCorporationAction.Enabled = true;
                                        ddlRegEmisiType.Enabled = false;
                                    }
                                    else
                                    {

                                        ddlCorporationAction.SelectedIndex = 0;
                                        ddlCorporationAction.Enabled = false;
                                    }

                                    ddlRegEmisiType.SelectedIndex = 0;
                                    ddlRegEmisiType.Enabled = false;
                                }
                                
                            }

                        }

                        //=================== end validation dropdownlist

#endregion

                        #region validation checkbox formula
                        if (recTariff.IsFormula)
                        {
                            ddlImpostionBase.Enabled = true;
                            txtPercentage.Enabled = true;
                            txtValue.Enabled = false;
                            txtMaxValue.Enabled = true;
                            txtMinValue.Enabled = true;
                            txtKelipatan.Enabled = true;
                        }
                        else
                        {
                            ddlImpostionBase.SelectedIndex = 0;
                            ddlImpostionBase.Enabled = false;
                            txtPercentage.Enabled = false;
                            txtValue.Enabled = true;
                            txtMaxValue.Enabled = false;
                            txtMinValue.Enabled = false;
                            txtKelipatan.Enabled = false;
                        }

                        #endregion
                    }
                }

                GetPrivilege();
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
}
}
